package com.ucl.service.flowsearch.impl;

import com.ucl.common.constant.CommonConstants;
import com.ucl.common.enumeration.ProductEnum;
import com.ucl.common.exception.CheckException;
import com.ucl.model.common.ApiResult;
import com.ucl.model.entity.flowsearch.FlowDataOption;
import com.ucl.model.entity.system.SysDict;
import com.ucl.model.request.flowsearch.FlowRequestOptionsQueryPara;
import com.ucl.model.request.flowsearch.flowDataUpdateRequest;
import com.ucl.model.vo.flowsearch.ColumnVO;
import com.ucl.model.vo.system.DictVO;
import com.ucl.service.flowsearch.FlowDataService;
import com.ucl.service.flowsearch.MifiService;
import com.ucl.service.flowsearch.OperatorService;
import com.ucl.service.flowsearch.SimService;
import com.ucl.service.system.SysDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FlowDataServiceImpl implements FlowDataService {

    @Autowired
    private SimService simService;

    @Autowired
    private MifiService mifiService;

    @Autowired
    private OperatorService operatorService;

    @Autowired
    private SysDictService sysDictService;

    @Override
    public ApiResult getRequestOptions(FlowRequestOptionsQueryPara filter) throws Exception {
        if (StringUtils.isEmpty(filter.getProduct())) {
            throw new CheckException(400,"产品分类必须选择");
        }
        if (StringUtils.isNotEmpty(filter.getSupplier())) {
            if (StringUtils.isEmpty(filter.getCategory())) {
                throw new CheckException(400,"业务类型必须选择");
            }
        }
        FlowDataOption flowDataOption = doRequestOptions(filter);
        return new ApiResult(flowDataOption);
    }

    @Override
    public List<DictVO> getProducts() {
        // 从字典表中找
        List<SysDict> sysDicts = sysDictService.selectDictByType(CommonConstants.DICT_PRODUCT_TYPE);
        List<DictVO> products = new ArrayList<>();
        for (SysDict sysDict : sysDicts) {
            DictVO vo = new DictVO();
            vo.setCode(sysDict.getDictCode());
            vo.setValue(sysDict.getDictValue());
            vo.setDesc(sysDict.getDescription());
            products.add(vo);
        }
        return products;
    }

    @Override
    public List<ColumnVO> getTableColumnNames(String tableName) {
        if (CommonConstants.TABLE_NAME_SIM.equals(tableName) || CommonConstants.TABLE_NAME_MIFI.equals(tableName)
        || CommonConstants.TABLE_NAME_OPERATOR.equals(tableName)) {
            List<ColumnVO> tableColumnNames = simService.getTableColumnNames(tableName);
            List<ColumnVO> columnNames = new ArrayList();
            for (ColumnVO tableColumnName : tableColumnNames) {
                if (tableColumnName.getName().equals("id") ||
                        tableColumnName.getName().equals("country") ||
                        tableColumnName.getName().equals("region") ||
                        tableColumnName.getName().equals("supplier") ||
                        tableColumnName.getName().equals("create_time") ||
                        tableColumnName.getName().equals("is_delete")
                ) {
                    continue;
                }
                columnNames.add(tableColumnName);
            }
            return columnNames;
        }
        return null;
    }

    @Override
    public int flowDataUpdate(flowDataUpdateRequest filter) {
        int result = 0;
        ProductEnum pEnum = ProductEnum.getByName(filter.getFlowType());
        switch (pEnum) {
            case PRODUCT_SIM:
                result = simService.flowDataUpdate(filter);
                break;
            case PRODUCT_MIFI:
                result = mifiService.flowDataUpdate(filter);
                break;
            case PRODUCT_OPERATOR:
                result = operatorService.flowDataUpdate(filter);
                break;
            case PRODUCT_OTHER:
                break;
            default:
                break;
        }

        return result;
    }

    @Override
    public int flowDataDelete(flowDataUpdateRequest filter) {
        int result = 0;
        ProductEnum pEnum = ProductEnum.getByName(filter.getFlowType());
        switch (pEnum) {
            case PRODUCT_SIM:
                result = simService.flowDataDelete(filter);
                break;
            case PRODUCT_MIFI:
                result = mifiService.flowDataDelete(filter);
                break;
            case PRODUCT_OPERATOR:
                result = operatorService.flowDataDelete(filter);
                break;
            case PRODUCT_OTHER:
                break;
            default:
                break;
        }

        return result;
    }

    /*private ApiResult getRequestOptions(String product) throws CheckException {
        return getRequestOptions(product, null);
    }

    private ApiResult getRequestOptions(String product, String category) throws CheckException {
        return getRequestOptions(product, category, null);
    }

    private ApiResult getRequestOptions(String product, String category, String supplier) throws CheckException {
        if (StringUtils.isEmpty(product)) {
            throw new CheckException(400,"产品分类必须选择");
        }
        if (StringUtils.isNotEmpty(supplier)) {
            if (StringUtils.isEmpty(category)) {
                throw new CheckException(400,"业务类型必须选择");
            }
            doRequestOptions(product, category, supplier);
            return null;
        }
        if (StringUtils.isNotEmpty(category)) {
            doRequestOptions(product, category, supplier);
            return null;
        } else {

            doRequestOptions(product, category, supplier);
            return null;
        }

        //return ApiResult.ok();
    }*/

    private FlowDataOption doRequestOptions(FlowRequestOptionsQueryPara filter) {
        FlowDataOption flowDataOption = new FlowDataOption();
        ProductEnum pEnum = ProductEnum.getByName(filter.getProduct());
        switch (pEnum) {
            case PRODUCT_SIM:
                flowDataOption = simService.getRequestOptions(filter);
                break;
            case PRODUCT_MIFI:
                flowDataOption = mifiService.getRequestOptions(filter);
                break;
            case PRODUCT_OPERATOR:
                flowDataOption = operatorService.getRequestOptions(filter);
                break;
            case PRODUCT_OTHER:
                break;
            default:
                break;
        }
        return flowDataOption;
    }


}
